package com.meviewer.service;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.google.gson.JsonArray;
import com.google.gson.JsonParser;
import com.meviewer.common.FileUtil;

import java.io.File;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.UUID;

public class FindPosService {
    public static List<Map<String,Object>> posList = new ArrayList<>();
    public static void findGeoJsonDataOrJsonData1(List<Map<String,Object>> list, String path) {
        File file = new File(path);
        //判断file对象是否为空
        if(file.isDirectory()) {
            //用数组来获取这个目录下的子文件夹
            File[] fs = file.listFiles();
            for(File f : fs) {
                //递归遍历所有子文件夹
                findGeoJsonDataOrJsonData1(list,f.getAbsolutePath());
            }
        }else if(file.isFile() && file.getName().endsWith("geojson") || file.isFile() && file.getName().endsWith("json")){
            Map<String,Object> map = new HashMap<>();
            String read = FileUtil.read(file.getAbsolutePath());
            map.put("suffix",file.getName().substring(file.getName().lastIndexOf(".")+1));
            map.put("data",read);
            map.put("name",file.getName());
            list.add(map);
        }
        posList = list;
    }

    public static List<Map<String, Object>> isFindPosData(Map<String, Object> map, String name) {
        List<Map<String,Object>> listr = new ArrayList<>();
          Objects.requireNonNull(map.get("suffix")).toString();
                JSONObject j = (JSONObject) JSONObject.parse((Objects.requireNonNull(map.get("data"))).toString());
                JsonArray geoArray=new JsonParser().parse(Objects.requireNonNull(j.get("features")).toString()).getAsJsonArray();

                for (Object o : geoArray) {
                    JSONObject jsonObject = (JSONObject) JSONObject.parse(o.toString());
                    JSONObject jsonObject1 = (JSONObject) JSONObject.parse(Objects.requireNonNull(jsonObject.get("properties")).toString());
                    Map<String,Object> resM = new HashMap<>();
                    resM.put("res","false");
                    if(Objects.requireNonNull(jsonObject1.get("name")).toString().contains(name)){
                        JSONObject geometry = (JSONObject) JSONObject.parse(Objects.requireNonNull(jsonObject.get("geometry")).toString());
                        String coordinates = Objects.requireNonNull(geometry.get("coordinates")).toString();
                        String r1 = coordinates.replace("[", "");
                        String r2 = r1.replace("]", "");
                        String[] split = r2.split(",");
                        resM.put("lon",split[0]);
                        resM.put("lat",split[1]);
                        resM.put("posName",jsonObject1.get("name"));
                        resM.put("res","true");
                        resM.put("uuid",UUID.randomUUID().toString());
                        listr.add(resM);
                    }
                }


        return listr;
    }


    public static String getFileForlderNamesByPosName(String name) {
        JSONArray jsonArray = new JSONArray();


        for (Map<String, Object> map : posList) {

            List<Map<String,Object>> mm = isFindPosData(map,name);
            float a= 0 ;

            for (Map<String, Object> stringObjectMap : mm) {
                a= a+1;
                if (Objects.requireNonNull(stringObjectMap.get("res")).toString().equals("true")) {
                    JSONObject jo = new JSONObject();
                    jo.put("lon",stringObjectMap.get("lon"));
                    jo.put("lat",stringObjectMap.get("lat"));
                    jo.put("uuid",stringObjectMap.get("uuid").toString());
                    jo.put("posName",stringObjectMap.get("posName"));
                    jsonArray.add(jo);

                    while (a  == mm.size()){
                        return jsonArray.toJSONString();
                    }
                }
            }
        }

        return jsonArray.toJSONString();
    }
}
